home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / onetime.zip / FCODE.C < prev    next >
C/C++ Source or Header  |  1995-04-26  |  3KB  |  137 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <io.h>
  5.  
  6. #define GROUP 5
  7. #define SPACE ' '
  8. #define MAX_INPUT_LEN 30
  9. #define INVOCATION_ERROR 20
  10. #define CMD *argv[1]
  11. #define TRUE 1
  12. #define FALSE 0
  13. #define MAXVAL 256
  14. #define FILE_ERR 11
  15. #define BUFFERSIZE 8192
  16.  
  17. #define ISCODE(x) x<MAXVAL?TRUE:FALSE
  18.  
  19. typedef enum { ENCODE, DECODE } Codeflag;
  20.  
  21. int convert( int ch, Codeflag C );
  22. int code( Codeflag Operation);
  23.  
  24.  
  25.  
  26.  
  27.  
  28. void main( int argc, char **argv )
  29. {
  30.    char answer[ MAX_INPUT_LEN ];
  31.  
  32.    if( CMD == 'e' || CMD == 'E' )
  33.      code( ENCODE );
  34.    else
  35.      if( CMD == 'd' || CMD == 'D' )
  36.         code ( DECODE );
  37.    else
  38.      {
  39.      puts( "Encode or Decode? ");
  40.      gets( answer );
  41.  
  42.      if( *answer == 'e' || *answer == 'E' )
  43.         code ( ENCODE );
  44.      else
  45.         if( *answer == 'd' || *answer == 'D' )
  46.           code ( DECODE );
  47.      else
  48.         exit ( INVOCATION_ERROR );
  49.  
  50.      }
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
  57. int code( Codeflag Operation)
  58. {
  59.  
  60.    register unsigned char c;
  61.    register int ck;
  62.    long flength,
  63.         number = 0;  //Number of characters operated on.
  64.  
  65.    FILE *plaintxt,
  66.        *codekey,
  67.        *encfile,
  68.        *filename;
  69.  
  70.    if( NULL == ( codekey = fopen( "code.key", "rb" ) ) )
  71.        exit ( FILE_ERR );
  72.    if( setvbuf( codekey, NULL, _IOFBF, BUFFERSIZE ) )
  73.          exit( FILE_ERR );
  74.  
  75.    if( Operation == ENCODE )
  76.     {
  77.       if( NULL == ( plaintxt = fopen( "plain.txt", "rb" ) ) )
  78.           exit( FILE_ERR );
  79.       if( setvbuf( plaintxt, NULL, _IOFBF, BUFFERSIZE ) )
  80.          exit( FILE_ERR );
  81.  
  82.       if( NULL == ( encfile = fopen( "encr.xxx", "ab" ) ) )
  83.           exit ( FILE_ERR );
  84.       if( setvbuf( encfile, NULL, _IOFBF, BUFFERSIZE ) )
  85.          exit( FILE_ERR );
  86.  
  87.     }
  88.    else
  89.     {
  90.       if( NULL == ( plaintxt = fopen( "decr.txt", "wb" ) ) )
  91.           exit( FILE_ERR );
  92.       if( setvbuf( plaintxt, NULL, _IOFBF, BUFFERSIZE ) )
  93.          exit( FILE_ERR );
  94.  
  95.       if( NULL == ( encfile = fopen( "encr.xxx", "rb" ) ) )
  96.           exit( FILE_ERR );
  97.       if( setvbuf( encfile, NULL, _IOFBF, BUFFERSIZE ) )
  98.          exit( FILE_ERR );
  99.     }
  100.  
  101.   if( Operation == ENCODE )
  102.     filename = plaintxt;
  103.   else
  104.     filename = encfile;
  105.  
  106.  
  107.  
  108.       flength = filelength( fileno( filename ) );
  109.  
  110.  
  111.    while( number < flength )
  112.        {
  113.       c = fgetc( filename );
  114.        number++;
  115.  
  116.        ck = fgetc( codekey );
  117.  
  118.        if( Operation == ENCODE )
  119.          c += ck;
  120.        else
  121.          c -= ck;
  122.  
  123.        if( Operation == ENCODE )
  124.          fputc( c, encfile );
  125.        else
  126.          fputc( c, plaintxt );
  127.  
  128.        }
  129.  
  130.    fcloseall();
  131.  
  132.    return( number );
  133.  
  134. }
  135.  
  136.  
  137.